//
//  XYJZTTableViewCell.m
//  徙游记-01
//
//  Created by hcios on 2017/7/10.
//  Copyright © 2017年 鹿林. All rights reserved.
//

#import "XYJZTTableViewCell.h"
#import "UIImageView+WebCache.h"

static NSString *const LRPerceivedErrorId = @"PerceivedErrorId";


@interface XYJZTTableViewCell()

@property (weak, nonatomic) IBOutlet UIImageView *bgImageView;

@property (weak, nonatomic) IBOutlet UILabel *titleLable;

@property (weak, nonatomic) IBOutlet UILabel *textLable;

@end

@implementation XYJZTTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self setupSubViews];
    self.clipsToBounds = YES;
}


-(void)setupSubViews{
    
    _coverView = [[UIView alloc] initWithFrame:self.bgImageView.frame];
    _coverView.backgroundColor = [UIColor blackColor];
    _coverView.alpha = 0.1;
    [self.bgImageView addSubview:_coverView];
    
    [_titleLable setFont:[UIFont fontWithName:FONT_NAME_LTH size:18]];
    [_titleLable setTextColor:[UIColor whiteColor]];
    _titleLable.shadowColor = [UIColor grayColor];
    _titleLable.shadowOffset = CGSizeMake(1, 1);
    [self.bgImageView addSubview:_titleLable];
    
    [_textLable setFont:[UIFont fontWithName:FONT_NAME_LTH size:18]];
    [_textLable setTextColor:[UIColor whiteColor]];
    _textLable.shadowColor = [UIColor grayColor];
    _textLable.shadowOffset = CGSizeMake(1, 1);
    [self.bgImageView addSubview:_textLable];
}

+(instancetype)perceicedErrorCellFromXib:(UITableView *)tableView{
    XYJZTTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:LRPerceivedErrorId];
    
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


-(void)cellOnTableView:(UITableView *)tableView didScrollView:(UIView *)view{
    
    CGRect rect = [tableView convertRect:self.frame toView:view];
    
    float distanceCenter = CGRectGetHeight(view.frame) / 2 - CGRectGetMinY(rect);
    
    float difference = CGRectGetHeight(self.bgImageView.frame) - CGRectGetHeight(self.frame);
    
    float imageMove = (distanceCenter / CGRectGetHeight(view.frame)) * difference;
    
    CGRect imageRect = self.bgImageView.frame;
    
    imageRect.origin.y = imageMove - (difference / 2);
    
    self.bgImageView.frame = imageRect;
    
    _coverView.alpha = 0.45 - ((imageRect.origin.y + 100) * 0.005);
}

-(void)setModel:(XYJZTModel *)model{
    [self.bgImageView sd_setImageWithURL:[NSURL URLWithString:model.zt_image_url] placeholderImage:[UIImage imageNamed:@"zhanWeiTu"]];
    self.titleLable.text = model.zt_name;
    _textLable.text = model.zt_title;
}

@end
